#ifndef _DEV_BY25Q64_H_
#define _DEV_BY25Q64_H_

#include "includes.h"
#include "DRV_SPI1.h"

//********************************引脚初始化*********************************************//



//********************************数组的长度*********************************************//
#define DEV_FLASH_DATA_LEN                  (4096)
//********************************数组的长度*********************************************//

//********************************FLASH存储芯片的指令************************************//
#define BY25Q64_WriteEnable                 0x06
#define BY25Q64_WriteDisable                0x04
#define BY25Q64_ReadStatusRegister1         0x05  //S7-S0
#define BY25Q64_ReadStatusRegister2         0x35  //S15-S8
#define BY25Q64_ReadStatusRegister3         0x15  //S23-S16
#define BY25Q64_WriteStatusRegister1        0x01 
#define BY25Q64_WriteStatusRegister2        0x31 
#define BY25Q64_WriteStatusRegister3        0x11 
#define BY25Q64_ReadData                    0x03
#define BY25Q64_FastRead                    0x0B
#define BY25Q64_PageProgram                 0x02
#define BY25Q64_QuadPageProgram             0x32
#define BY25Q64_SectorErase                 0x20
#define BY25Q64_BlockErase32                0x52
#define BY25Q64_BlockErase64                0xD8
#define BY25Q64_ChipErase                   0xC7
#define BY25Q64_ReadIdentification          0x9F
#define BY25Q64_DeepPowerDown               0xB9
#define BY25Q64_ManufacturerDeviceID        0x90
#define BY25Q64_ReleaseFromDeepPowerDown    0xAB
#define BY25Q64_ReadIdentification          0x9F
//********************************FLASH存储芯片的指令**********************************//

//********************************FLASH存储芯片的典型速度******************************//
//Program/Erase Speed
#define PageProgramTime                         (0.6)   //ms
#define SectorEraseTime                         (50)
#define BlockEraseTime                          (200)
#define ChipEraseTime                           (25000)
//********************************GD25存储芯片的典型速度******************************//


//********************************FLASH存储芯片的片区大小****************************//
//Flexible Architecture   1M=1024K  1K=1024Byte 1Byte=8bit 
#define BY25Q64_Size                            (8*1024*1024)       //Byte
#define BY25Q64_PageSize                        (256)               //Byte
#define BY25Q64_SectorSize                      (4*1024)            //Byte
#define BY25Q64_BlockSize                       (32*1024)           //Byte
//********************************FLASH存储芯片的片区大小****************************//

#define DEV_FLASH_ADDRESS_INFO 			        ((uint32_t)0x00)
#define DEV_FLASH_ADDRESS_TASK    			    ((uint32_t)0x1000)
#define DEV_FLASH_ADDRESS_GLOBALTASK  		    ((uint32_t)0x41000)
#define DEV_FLASH_ADDRESS_BOOTLOADER_INFO 	    ((uint32_t)0x81000)
#define DEV_FLASH_ADDRESS_BOOTLOADER_APP1 	    ((uint32_t)0x82000)
#define DEV_FLASH_ADDRESS_BOOTLOADER_BACKUP     ((uint32_t)0xA2000)
 
 
//********************************片选引脚的宏定义******************************//
#define DEV_FLASH_CS_LOW()			            FLASH_SPI_CS_LOW()
#define DEV_FLASH_CS_HIGH() 			        FLASH_SPI_CS_HIGH()
//********************************片选引脚的宏定义******************************//
 

//********************************推荐常用函数******************************//
//SPI初始化
void by25q64SPIInit(void);
//刷新写入，先删除再写入（自动处理删除，和写入跨页）
void by25q64FlushWrite(uint8_t	*sourcedata,uint32_t address,uint32_t length);
//读数据
void by25q64ReadData(uint8_t *sourcedata, uint32_t address, uint32_t length);
//删除（删除指定地址到指定长度，自动处理跨页）
void by25q64FlushDel(uint32_t address,uint32_t length);
//********************************推荐常用函数******************************//


//********************************不常用函数******************************//
//擦除片
void by25q64EraseSector(uint32_t eraseAddr);

//擦除范围片
void by25q64EraseAppointSector(uint32_t addstart, uint32_t addend);

//擦除整块芯片
void Flash_EraseChip(void);

//写数据
void by25q64WriteData(uint8_t *sourcedata, uint32_t address, uint32_t length);

//获取位置信息（用于变地址存储信息，减少芯片的擦写次数）
uint32_t by25q64GetDataPos(uint32_t Startaddress,uint32_t length);

//读状态寄存器（用于配置外挂FLASH的一些功能）
void by25q64WriteStatusRegisterx(uint8_t sourcedata, uint8_t GD25Q64_ReadStatusRegisterx);

//********************************不常用函数******************************//
void BY25Q64_Debug(void);


#endif

